package adapter;/*
 *@创建者 lnn
 *@创建时间2016/9/121:57
 *@描述 ${TODO}
*/

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.boltshow.R;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

import model.Car;

public class CarBaseAdapter extends BaseAdapter {

    private List<Car> mData;
    private Context mContext;
    private ImageOptions mImageOptions;
    private View.OnClickListener mListener;
    private CompoundButton.OnCheckedChangeListener mCheckListener;

    public CarBaseAdapter(List<Car> data, Context context) {
        mData = data;
        mContext = context;
        mListener = (View.OnClickListener) context;
        mCheckListener = (CompoundButton.OnCheckedChangeListener) context;
        mImageOptions = new ImageOptions.Builder().setUseMemCache(true).build();
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_car_one, null);
            holder = new MyViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        if (position == mData.size() - 1) {
            holder.llContainer.setVisibility(View.VISIBLE);
        } else {
            holder.llContainer.setVisibility(View.GONE);
        }
        Car car = mData.get(position);
        x.image().bind(holder.ivIcon, car.getIcon(), mImageOptions);
        holder.tvInfo.setText(car.getGoodName());
        holder.tvChima.setText("尺码:" + car.getSize());
        holder.tvColor.setText("颜色:" + car.getColor());
        holder.tvPrice.setText("¥" + car.getPrices());
        holder.tvCount.setText(car.getCount() + "");
        //给checkbox设置选中事件(设置了listener之后会系统会立刻调用监听,传一个false过去)
        holder.cbSelect.setTag(position);
        holder.cbSelect.setOnCheckedChangeListener(mCheckListener);
        //设置checkBox的选中状态
        holder.cbSelect.setChecked(car.isCheck);
        //给减少和增加设置监听事件,同时增加tag
        holder.ivAdd.setTag(position);
        holder.ivSub.setTag(position);
        holder.ivAdd.setOnClickListener(mListener);
        holder.ivSub.setOnClickListener(mListener);
        //给删除做监听
        holder.ivDelete.setTag(position);
        holder.ivDelete.setOnClickListener(mListener);
        //计算最下面的
        holder.tvCountGoods.setText("共" + mData.size() + "件商品 下单闪降");
        if (mData != null && mData.size() != 0) {
            int sum = 0;
            for (int i = 0; i < mData.size(); i++) {
                if (mData.get(i).isCheck) {
                    sum += mData.get(i).getCutPrice();
                }
            }
            for (int i = 0; i < mData.size(); i++) {
                boolean isCheck = mData.get(i).isCheck;
                if (isCheck) {
                    holder.tvJisuan.setText(getMoneyOne() + "-" + sum + "=¥" + (getMoneyOne() - sum));
                    break;
                } else {
                    holder.tvJisuan.setText("你还未勾选任何商品");
                }
            }
        }
        return convertView;
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout llContainer;
        private CheckBox cbSelect;
        private ImageView ivIcon, ivAdd, ivSub, ivDelete;
        private TextView tvInfo, tvChima, tvColor, tvPrice, tvCount;
        private TextView tvCountGoods, tvJisuan;

        public MyViewHolder(View itemView) {
            super(itemView);
            llContainer = (LinearLayout) itemView.findViewById(R.id.ll_item_car_container);

            cbSelect = (CheckBox) itemView.findViewById(R.id.cb_item_car_check);
            ivAdd = (ImageView) itemView.findViewById(R.id.iv_item_car_add);
            ivDelete = (ImageView) itemView.findViewById(R.id.iv_item_car_delete);
            ivIcon = (ImageView) itemView.findViewById(R.id.iv_item_car_icon);
            ivSub = (ImageView) itemView.findViewById(R.id.iv_item_car_sub);
            tvInfo = (TextView) itemView.findViewById(R.id.tv_item_car_info);
            tvChima = (TextView) itemView.findViewById(R.id.tv_item_car_chima);
            tvColor = (TextView) itemView.findViewById(R.id.tv_item_car_color);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_item_car_price);
            tvCount = (TextView) itemView.findViewById(R.id.tv_item_car_count);
            tvCountGoods = (TextView) itemView.findViewById(R.id.tv_item_car_countgood);
            tvJisuan = (TextView) itemView.findViewById(R.id.tv_item_car_jisuan);
        }
    }

    public int getMoney() {
        int money = 0;
        if (mData == null && mData.size() == 0) {
            return money;
        }
        for (int i = 0; i < mData.size(); i++) {
            Car car = mData.get(i);
            if (car.isCheck) {
                int prices = Integer.parseInt(car.prices);
                int itemMoney = car.count * prices;
                money += itemMoney;
            }
        }
        int sum = 0;
        for (int i = 0; i < mData.size(); i++) {
            if (mData.get(i).isCheck) {
                sum += mData.get(i).getCutPrice();
            }
        }
        return money - sum;
    }

    public int getMoneyOne() {
        int money = 0;
        if (mData == null && mData.size() == 0) {
            return money;
        }
        for (int i = 0; i < mData.size(); i++) {
            Car car = mData.get(i);
            if (car.isCheck) {
                int prices = Integer.parseInt(car.prices);
                Log.i("TAG", "prices" + prices);
                int itemMoney = car.count * prices;
                money += itemMoney;
            }
        }

        return money;
    }
}
